/**
 * 
 */
package jp.co.wap.exam;
import java.util.List;

import java.util.Arrays;
import jp.co.wap.exam.lib.Interval;
/**
 * @author pratibind_jha
 *
 */
public class Problem1 {
	
	public int getMaxIntervalOverlapCount(List<Interval> intervals) {
		// TODO: Implement this method.
		int len = intervals.size();
		float[] begin = new float[len]; //float array for all begning time
		float[] end = new float[len]; //float aray for end time.
		float hour, min;
		int i=0,startPtr=0,endPtr=0,m=0,fullLength=0;
		int global[] = new int[2*len];
		
		
		for(Interval intval : intervals) {
			hour = intval.getBeginHour();
			min = intval.getBeginMinute();
			
			//put dd.dd formated vales in begin array
			begin[i] = (float)hour+(float)(min/100);
			
			hour = intval.getEndHour();
			min = intval.getEndMinute();
			
			//put dd.dd formated vales in end array
			//System.out.println("minute: "+ min);
			end[i++] = (float)hour+(float)(min/100);
			//System.out.println("end "+ i + " "+ end[i-1]);
		}
		
		/*System.out.println("Before sort");
		for(float j: begin)
			System.out.println(j);
		for(float j: end)
			System.out.println(j);
		*/
		
		Arrays.sort(begin);
		Arrays.sort(end);
		
		/*System.out.println("After sort");
		System.out.println("Begin");
		for(float j: begin)
			System.out.println(j);
		System.out.println("End");
		for(float j: end)
			System.out.println(j);*/
		 		
		
		//merge the sorted array into one global array with some specific 
		//convention 1 for start and -1 for end;
		
		while((startPtr<len) && (endPtr<len)) {
				if(begin[startPtr] < end[endPtr]) {
						int x=1;
						global[fullLength++] = x;  
								
						startPtr++;
				}		
				else if(begin[startPtr] >= end[endPtr]) {
					int y = -1;	
					global[fullLength++] =  y;
					endPtr++;
			}
		}
		while(startPtr<len) {
			int x = 1;
			global[fullLength++] =  x;
			startPtr++;
		}	
		
		while(endPtr<len) {
			int y = -1;
			global[fullLength++] =  y;
			endPtr++;
		}
		
		/*for(int j: global)
				System.out.println(j);*/
		
		int max=0,currSum=0;
		//now find the overlap
		for(int index=0; index<2*len; index++) {
		
			currSum = currSum + global[index];
			
			if(currSum > max)
					max = currSum;
			
			if(currSum < 0)
					currSum=0;
				
		}
		
		//return the maximum overlap
		return max;
	}

}
